import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class PrinterTemplateService extends BaseService<any> {
  private readonly logger = new Logger(PrinterTemplateService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: PrinterTemplateService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'template_id,template_type,template_name,value,create_time';         $order = 'create_time desc';          $search_model = $this->model->wher...
const field = 'template_id,template_type,template_name,value,create_time';
        const order = 'create_time desc';

        const search_model = this.model.where([ [ 'site_id', "=", this.site_id ) )).withSearch([ "template_id", "template_type", "template_name" ), where).field(field).order(order).append([ 'template_type_name' )];
        const list = this.pageQuery(search_model);
        return list;
    }

    /**
     * 获取小票打印模板列表
     * @param where
     * @param string field
     * @return * @throws \think\db\exception\DbBusinessException
     */
    async getList(where = [), field = 'template_id,template_type,template_name,value,create_time')
    {
        const order = 'create_time desc';
        return this.model.where([ [ 'site_id', "=", this.site_id ) )).withSearch([ "template_id", "template_type", "template_name" ), where).field(field).order(order).append([ 'template_type_name' )).select().toArray(];
    }

    /**
     * 获取小票打印模板信息
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'template_id,site_id,template_type,template_name,value';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加小票打印模板
     * @param data
     * @return mixed
     */
    async add(data)
    {
        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.template_id;
    }

    /**
     * 小票打印模板编辑
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'template_id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除小票打印模板
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        // 检测要删除的模板有没有被打印机使用
        const field = 'template_type';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];

        const core_printer_service = this.corePrinterService;
        const printer_list = core_printer_service.getList([
            [ 'site_id', '=', this.site_id ],
            [ 'template_type', 'like', '%"' . info[ 'template_type' ]  + '"%' )
        ), 'printer_id,printer_name,value'];

        if (!!printer_list)) {
            foreach (printer_list as k => v) {
                if (!!v[ 'value' ))) {
                    foreach (v[ 'value' ) as value_k => value_v) {
                        foreach (value_v as trigger_k => trigger_v) {
                            if (trigger_v[ 'template_id' ) == id) {
                                throw new BusinessException("该模板已被打印机 [{v.printer_name}) 使用，无法删除";
  }
                    }
                }
            }
  }

  /**
   * getList
   * 对应 PHP: PrinterTemplateService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(where: any[], field: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: $order = 'create_time desc';         return $this->model->where([ [ 'site_id', "=", $this->site_id ] ])->withSearch([ "template_id", "template_type", ...
const order = 'create_time desc';
        return this.model.where([ [ 'site_id', "=", this.site_id ) )).withSearch([ "template_id", "template_type", "template_name" ), where).field(field).order(order).append([ 'template_type_name' )).select().toArray(];
    }

    /**
     * 获取小票打印模板信息
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'template_id,site_id,template_type,template_name,value';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加小票打印模板
     * @param data
     * @return mixed
     */
    async add(data)
    {
        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.template_id;
    }

    /**
     * 小票打印模板编辑
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'template_id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除小票打印模板
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        // 检测要删除的模板有没有被打印机使用
        const field = 'template_type';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];

        const core_printer_service = this.corePrinterService;
        const printer_list = core_printer_service.getList([
            [ 'site_id', '=', this.site_id ],
            [ 'template_type', 'like', '%"' . info[ 'template_type' ]  + '"%' )
        ), 'printer_id,printer_name,value'];

        if (!!printer_list)) {
            foreach (printer_list as k => v) {
                if (!!v[ 'value' ))) {
                    foreach (v[ 'value' ) as value_k => value_v) {
                        foreach (value_v as trigger_k => trigger_v) {
                            if (trigger_v[ 'template_id' ) == id) {
                                throw new BusinessException("该模板已被打印机 [{v.printer_name}) 使用，无法删除";
  }
                    }
                }
            }
  }

  /**
   * getInfo
   * 对应 PHP: PrinterTemplateService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'template_id,site_id,template_type,template_name,value';          $info = $this->model->field($field)->where([ [ 'template_id', "=", $id ] ])...
const field = 'template_id,site_id,template_type,template_name,value';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加小票打印模板
     * @param data
     * @return mixed
     */
    async add(data)
    {
        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.template_id;
    }

    /**
     * 小票打印模板编辑
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'template_id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除小票打印模板
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        // 检测要删除的模板有没有被打印机使用
        const field = 'template_type';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];

        const core_printer_service = this.corePrinterService;
        const printer_list = core_printer_service.getList([
            [ 'site_id', '=', this.site_id ],
            [ 'template_type', 'like', '%"' . info[ 'template_type' ]  + '"%' )
        ), 'printer_id,printer_name,value'];

        if (!!printer_list)) {
            foreach (printer_list as k => v) {
                if (!!v[ 'value' ))) {
                    foreach (v[ 'value' ) as value_k => value_v) {
                        foreach (value_v as trigger_k => trigger_v) {
                            if (trigger_v[ 'template_id' ) == id) {
                                throw new BusinessException("该模板已被打印机 [{v.printer_name}) 使用，无法删除";
  }
                    }
                }
            }
  }

  /**
   * add
   * 对应 PHP: PrinterTemplateService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data[ 'site_id' ] = $this->site_id;         $res = $this->model->create($data);         return $res->template_id;     }      /**      * 小票打印模板编辑     ...
data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.template_id;
    }

    /**
     * 小票打印模板编辑
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'template_id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除小票打印模板
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        // 检测要删除的模板有没有被打印机使用
        const field = 'template_type';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];

        const core_printer_service = this.corePrinterService;
        const printer_list = core_printer_service.getList([
            [ 'site_id', '=', this.site_id ],
            [ 'template_type', 'like', '%"' . info[ 'template_type' ]  + '"%' )
        ), 'printer_id,printer_name,value'];

        if (!!printer_list)) {
            foreach (printer_list as k => v) {
                if (!!v[ 'value' ))) {
                    foreach (v[ 'value' ) as value_k => value_v) {
                        foreach (value_v as trigger_k => trigger_v) {
                            if (trigger_v[ 'template_id' ) == id) {
                                throw new BusinessException("该模板已被打印机 [{v.printer_name}) 使用，无法删除";
  }
                    }
                }
            }
  }

  /**
   * edit
   * 对应 PHP: PrinterTemplateService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $this->model->where([ [ 'template_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);         return true;     }      /**      * 删除...
this.model.where([ [ 'template_id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除小票打印模板
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        // 检测要删除的模板有没有被打印机使用
        const field = 'template_type';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];

        const core_printer_service = this.corePrinterService;
        const printer_list = core_printer_service.getList([
            [ 'site_id', '=', this.site_id ],
            [ 'template_type', 'like', '%"' . info[ 'template_type' ]  + '"%' )
        ), 'printer_id,printer_name,value'];

        if (!!printer_list)) {
            foreach (printer_list as k => v) {
                if (!!v[ 'value' ))) {
                    foreach (v[ 'value' ) as value_k => value_v) {
                        foreach (value_v as trigger_k => trigger_v) {
                            if (trigger_v[ 'template_id' ) == id) {
                                throw new BusinessException("该模板已被打印机 [{v.printer_name}) 使用，无法删除";
  }
                    }
                }
            }
  }

  /**
   * del
   * 对应 PHP: PrinterTemplateService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: // 检测要删除的模板有没有被打印机使用         $field = 'template_type';          $info = $this->model->field($field)->where([ [ 'template_id', "=", $id ] ])->findOrEmp...
// 检测要删除的模板有没有被打印机使用
        const field = 'template_type';

        const info = this.model.field(field).where([ [ 'template_id', "=", id ) )).findOrEmpty().toArray(];

        const core_printer_service = this.corePrinterService;
        const printer_list = core_printer_service.getList([
            [ 'site_id', '=', this.site_id ],
            [ 'template_type', 'like', '%"' . info[ 'template_type' ]  + '"%' )
        ), 'printer_id,printer_name,value'];

        if (!!printer_list)) {
            foreach (printer_list as k => v) {
                if (!!v[ 'value' ))) {
                    foreach (v[ 'value' ) as value_k => value_v) {
                        foreach (value_v as trigger_k => trigger_v) {
                            if (trigger_v[ 'template_id' ) == id) {
                                throw new BusinessException("该模板已被打印机 [{v.printer_name}) 使用，无法删除";
  }
                    }
                }
            }
  }
}
